package com.wn.servlet;

import com.wn.bean.User;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author yellow Docter
 * @date 2022 -07-05
 * @desc
 */
@WebServlet("/login")
public class LoginServlet extends javax.servlet.http.HttpServlet {

    protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        this.doGet(request, response);
    }

    protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {

        HttpSession session = request.getSession();

        //01 拿到用户名
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        User user = null;

        //02 判断 用户和密码
        // 普通用户
        if (username.equals("jack")&& password.equals("123")){
            user = new User();
            user.setUsername(username);
            user.setPassword(password);
            user.setGrade(1);
        }

        //管理员
        if (username.equals("rose")&& password.equals("123")){
            user = new User();
            user.setUsername(username);
            user.setPassword(password);
            user.setGrade(2);
        }

        //03 登录成功后把用户名存储到session中
        if (user != null){
            session.setAttribute("user",user);
        }

        // 转发到主页
        request.getRequestDispatcher("/index.jsp").forward(request,response);
    }
}
